package com.cm.log_process.log_settings.controller;

import com.cm.log_process.log_common.annotation.OperateLog;
import com.cm.log_process.log_common.config.ConfigBean;
import com.cm.log_process.log_common.entity.Log;
import com.cm.log_process.log_common.entity.Result;
import com.cm.log_process.log_common.entity.StatusCode;
import com.cm.log_process.log_common.exp.NoIndexException;
import com.cm.log_process.log_common.exp.ParamNotMatchException;
import com.cm.log_process.log_settings.service.SettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Creator:郭兆鹏
 * Date:2019/12/26
 * 作用:对外提供的es配置接口
 */
@RestController
@RequestMapping("settings")
public class SettingController {

    @Autowired
    private SettingService settingService;

    //设置最大返回结果数
    @RequestMapping(value = "/setMaxRes/{indexName}/{maxRes}",method = RequestMethod.GET)
    @OperateLog(oType = "_setting",oName = "设置当前索引最大返回结果")
    public Result<Log> setMaxRes(@PathVariable String indexName,@PathVariable String maxRes) throws Exception {
        if(StringUtils.isEmpty(indexName)){
            throw new NoIndexException("Undefined route");
        }
        boolean f = settingService.setMaxResult(indexName,maxRes);
        if(f){
            return new Result<>(StatusCode.SUCCESS.getCode(),"修改成功");
        }else{
            return new Result<>(StatusCode.ERROR.getCode(),"更新失败");
        }
    }


    //设置排序字段  fieldData = true
    @RequestMapping(value = "/setFieldData/{indexName}/{fieldName}",method = RequestMethod.GET)
    @OperateLog(oType = "_setting",oName = "设置字段域可排序")
    public Result<Log> setFieldData(@PathVariable String indexName,@PathVariable String fieldName) throws Exception {
        if(StringUtils.isEmpty(indexName)||StringUtils.isEmpty(fieldName)){
            throw new ParamNotMatchException("Undefined route");
        }
        boolean f = settingService.setFieldData(indexName,fieldName);
        if(f){
            return new Result<>(StatusCode.SUCCESS.getCode(),"修改成功");
        }else{
            return new Result<>(StatusCode.ERROR.getCode(),"更新失败");
        }
    }



    @RequestMapping(value = "/createIndex/{indexName}",method = RequestMethod.GET)
    @OperateLog(oType = "_create",oName = "创建索引")
    public Result<Log> createIndex(@PathVariable String indexName) throws Exception {
       boolean f = settingService.createIndex(indexName);
        if(f){
            return new Result<>(StatusCode.SUCCESS.getCode(),"创建成功");
        }else{
            return new Result<>(StatusCode.ERROR.getCode(),"创建失败");
        }
    }
}
